package tk.mybatis.springboot.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.springboot.conf.UserInfoContextHolder;
import tk.mybatis.springboot.model.UserInfo;

@Slf4j
@RestController
public class TestThreadLocal {
    /**
     * 说明：
     * 1.单独的Thread执行完成后会把ThreadLocal设置为null
     * java.lang.Thread#exit()
     * 2.线程池的话会重新入池，假如不执行ThreadLocal remove方法，会复用线程里面的ThreadLocal,
     *  如果重新执行set方法，会把里面的key==null的复用。参见java.lang.ThreadLocal.ThreadLocalMap#set(java.lang.ThreadLocal, java.lang.Object)
     *  复用thread会导致造成脏数据
     * @param userInfo
     * @return
     */
    //-XX:MetaspaceSize=100M -Xms150m -Xmx150m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=D:/mayun/dump -XX:+PrintGCDetails
    @GetMapping("/tltest")
    public Object test(UserInfo userInfo) {
//        UserInfo userInfo=new UserInfo();
//        userInfo.setUsername("lrrrrrrr");
//        userInfo.setPassword("lrrrrrrr");
//        userInfo.setEmail("lrrrrrrr");
//        userInfo.setUsertype("lrrrrrrr");
        byte[] zd = new byte[1024 * 1024*10];
       // userInfo.setZd(zd);
        UserInfoContextHolder.setUserInfo(userInfo);
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info(("threadlocal :" + Thread.currentThread().getName() + "--" + userInfo.getUsername()));
        UserInfoContextHolder.remove();
        return userInfo;
    }
}
